﻿using System;
using System.Collections.Generic;

namespace mForum.Domain.DomainObject
{
    using mForum.Core.Infrastructure.Validation;
    using mForum.Core.Infrastructure.Resource;
    using mForum.Core.Extensions;

    public class User : BaseEntity, IEntity, IValidatable
    {        
        public virtual string Username { get; set; }

        public virtual string Password { get; set; }

        public virtual string Email { get; set; }

        public virtual string PasswordQuestion { get; set; }

        public virtual string PasswordAnswer { get; set; }

        public virtual bool isLockedOut { get; set; }

        public virtual DateTime LastActivityDate { get; set; }

        public virtual DateTime LastLoginDate { get; set; }

        public virtual DateTime LastLockedOutDate { get; set; }

        public virtual DateTime CreatedDate { get; set; }


        public virtual ValidationResult Validate()
        {
            ValidationResult validationResult = new ValidationResult();
            ValidationHelper<User> validationHelper = new ValidationHelper<User>();

            validationResult.AddError(validationHelper.CreateErrorIf(!String.IsNullOrEmpty(Username), f => f.Username, ResourceKey.Common.RequiredField));
            validationResult.AddError(validationHelper.CreateErrorIf(Username.Length >= 256, f => f.Username, ResourceKey.Common.FieldLength));

            validationResult.AddError(validationHelper.CreateErrorIf(!String.IsNullOrEmpty(Password), f => f.Password, ResourceKey.Common.RequiredField));
            validationResult.AddError(validationHelper.CreateErrorIf(Password.Length >= 256, f => f.Password, ResourceKey.Common.FieldLength));

            validationResult.AddError(validationHelper.CreateErrorIf(!String.IsNullOrEmpty(Email), f => f.Email, ResourceKey.Common.RequiredField));
            validationResult.AddError(validationHelper.CreateErrorIf(Email.IsEmail(), f => f.Email, ResourceKey.Common.InvalidEmail));

            validationResult.AddError(validationHelper.CreateErrorIf(!String.IsNullOrEmpty(PasswordQuestion), f => f.PasswordQuestion, ResourceKey.Common.RequiredField));
            validationResult.AddError(validationHelper.CreateErrorIf(PasswordQuestion.Length >= 256, f => f.PasswordQuestion, ResourceKey.Common.FieldLength));

            validationResult.AddError(validationHelper.CreateErrorIf(!String.IsNullOrEmpty(PasswordAnswer), f => f.PasswordAnswer, ResourceKey.Common.RequiredField));
            validationResult.AddError(validationHelper.CreateErrorIf(PasswordAnswer.Length >= 256, f => f.PasswordAnswer, ResourceKey.Common.FieldLength));

            return validationResult;
        }
    }
}
